// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.patent.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.dao.PatentProductClassDao;
import com.liuxinlong.modules.dao.PatentTrendsDao;
import com.liuxinlong.modules.dao.UserDao;
import com.liuxinlong.modules.entity.PatentProductClass;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.patent.service.PatentProductClassService;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 专利产品品类实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-10-07
 */
@Service
@Slf4j
public class PatentProductClassServiceImpl implements PatentProductClassService {
    @Autowired
    private PatentProductClassDao patentProductClassDao;

    @Autowired
    private PatentTrendsDao patentTrendsDao;

    @Autowired
    private UserDao userDao;

    @Override
    public List<Map<String, Object>> pageProductClassInfo(Map<String, Object> queryParam) {
        List<PatentProductClass> originList = patentProductClassDao.pageProductClassList(queryParam);
        return completeResult(originList, (int) queryParam.get("startNum"));
    }

    @Override
    public int countProductClass(Map<String, Object> queryParam) {
        return patentProductClassDao.getProductClassCount(queryParam);
    }

    @Override
    public void addProductClassInfo(PatentProductClass patentProductClass) {
        patentProductClass.setId(SnowFlake.nextIdStr());
        if (!ObjectUtils.isEmpty(patentProductClassDao.getProductClassByName(patentProductClass.getName()))) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "已存在相同产品品类名称！");
        }
        patentProductClass.setDefaultShow(0);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        patentProductClass.setCreateUser(currentUser.getId());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        patentProductClass.setCreateTime(currentTime);
        patentProductClassDao.insert(patentProductClass);
    }

    @Override
    public void updateProductClassInfo(PatentProductClass patentProductClass) {
        PatentProductClass oldInfo = patentProductClassDao.getProductClassByName(patentProductClass.getName());
        if (!ObjectUtils.isEmpty(oldInfo) && !StringUtils.equals(oldInfo.getId(), patentProductClass.getId())) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "已存在相同产品品类名称！");
        }
        oldInfo.setName(patentProductClass.getName());
        oldInfo.setPatentEngineer(patentProductClass.getPatentEngineer());
        oldInfo.setRemark(patentProductClass.getRemark());
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        oldInfo.setUpdateUser(currentUser.getId());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        oldInfo.setUpdateTime(currentTime);
        patentProductClassDao.updateById(oldInfo);
    }

    @Override
    public void deleteProductClassInfo(String id) {
        PatentProductClass patentProductClass = patentProductClassDao.selectById(id);
        if (ObjectUtils.isEmpty(patentProductClass)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "删除失败，产品品类不存在！");
        }
        if (patentProductClass.getDefaultShow() == 1) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "删除失败，产品品类为默认展示！");
        }
        if (patentTrendsDao.countClassTrends(id) > 0) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "删除失败，存在产品品类专利动态！");
        }
        patentProductClassDao.deleteById(id);
    }

    @Override
    public void updateProductClassStatus(String id) {
        PatentProductClass patentProductClass = patentProductClassDao.selectById(id);
        if (ObjectUtils.isEmpty(patentProductClass)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "设置默认展示失败，产品品类不存在！");
        }
        if (patentProductClass.getDefaultShow() == 1) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "设置默认展示失败，产品品类为默认展示！");
        }
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        PatentProductClass currentDefault = patentProductClassDao.getCurrentDefault();
        if (!ObjectUtils.isEmpty(currentDefault)) {
            currentDefault.setDefaultShow(0);
            currentDefault.setUpdateUser(currentUser.getId());
            currentDefault.setUpdateTime(currentTime);
            patentProductClassDao.updateById(currentDefault);
        }
        patentProductClass.setDefaultShow(1);
        patentProductClass.setUpdateUser(currentUser.getId());
        patentProductClass.setUpdateTime(currentTime);
        patentProductClassDao.updateById(patentProductClass);
    }

    /**
     * 补全数据
     *
     * @param originList 原始数据
     * @param startNum   起始值
     * @return 补全后数据
     */
    private List<Map<String, Object>> completeResult(List<PatentProductClass> originList, int startNum) {
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        Map<String,Map<String,String>> userMap = userDao.getUserMap();
        int sort = startNum + 1;
        for (PatentProductClass productClass : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(productClass);
            map.put("sort", sort);
            map.put("patentEngineerName",userMap.containsKey(productClass.getPatentEngineer()) ? userMap.get(productClass.getPatentEngineer()).get("name") : "");
            resultList.add(map);
            sort++;
        }
        return resultList;
    }


}
